home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / real.e < prev    next >
Text File  |  1997-04-13  |  5KB  |  275 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. expanded class  REAL
  5. --
  6. -- Note : corresponding C type is "float"
  7. --
  8. inherit
  9.    REAL_REF
  10.       redefine
  11.      infix "+", infix "-", infix "*", infix "/", infix "^",
  12.      prefix "+", prefix "-", valid_divisor, infix "<", 
  13.      infix "<=", infix ">", infix ">=", compare, one, 
  14.      zero, out_in_tagged_out_memory, fill_tagged_out_memory
  15.       end;
  16.    
  17. feature {ANY}
  18.  
  19.    infix "+" (other: REAL): REAL is
  20.      -- Add `other' to Current.
  21.       external "CSE"
  22.       end;
  23.  
  24.    infix "-" (other: REAL): REAL is
  25.      -- Subtract `other' from Current.
  26.       external "CSE"
  27.       end;
  28.  
  29.    infix "*" (other: REAL): REAL is
  30.      -- Multiply `other' by Current.
  31.       external "CSE"
  32.       end;
  33.    
  34.    infix "/" (other: REAL): REAL is
  35.      -- Divide Current by `other'.
  36.       external "CSE"
  37.       end;
  38.  
  39.    infix "^" (e: INTEGER): DOUBLE is
  40.      -- Raise Current to `e'-th power.
  41.       do
  42.      check
  43.         Current = 0.0 implies e > 0;
  44.      end;
  45.      Result := to_double ^ e; 
  46.       end;
  47.  
  48.    prefix "+" : REAL is
  49.       do
  50.      Result := Current
  51.       end;
  52.  
  53.    prefix "-": REAL is
  54.       external "CSE"
  55.       end;
  56.  
  57.    abs: REAL is
  58.       do
  59.      if Current < 0.0 then
  60.         Result := -Current;
  61.      else
  62.         Result := Current;
  63.      end;
  64.       end;
  65.          
  66.    infix "<" (other: REAL): BOOLEAN is
  67.      -- Is Current less than `other'?
  68.       external "CSE"
  69.       end;
  70.    
  71.    infix "<=" (other: REAL): BOOLEAN is
  72.      -- Is Current smaller or equal to `other'?
  73.       external "CSE"
  74.       end;
  75.    
  76.    infix ">" (other: REAL): BOOLEAN is
  77.      -- Is Current greater than `other'?
  78.       external "CSE"
  79.       end;
  80.    
  81.    infix ">=" (other: REAL): BOOLEAN is
  82.      -- Is Current greater or equal to `other'?
  83.       external "CSE"
  84.       end;
  85.    
  86.    compare(other: REAL): INTEGER is
  87.      -- Compare Current with `other'.
  88.      -- '<' <==> Result < 0
  89.      -- '>' <==> Result > 0
  90.      -- Otherwise Result = 0
  91.       do
  92.      if Current < other then
  93.         Result := -1
  94.      else
  95.         if Current > other then
  96.            Result := 1
  97.         end;
  98.      end;
  99.       end;
  100.  
  101.    valid_divisor(other: REAL): BOOLEAN is
  102.       do
  103.      Result := (other /= 0.0)
  104.       end;
  105.    
  106.    one: REAL is 1.0;
  107.    
  108.    zero: REAL is 0.0;
  109.    
  110.    floor: INTEGER is
  111.      -- Greatest integral value no greater than Current.
  112.       do
  113.      Result := to_double.floor;
  114.       ensure 
  115.      result_no_greater: Current >= Result;
  116.      close_enough: Current - Result < one;
  117.       end;
  118.       
  119.    ceiling: INTEGER is
  120.      -- Smallest integral value no smaller than Current.
  121.       do
  122.      Result := to_double.ceiling;
  123.       ensure
  124.      result_no_smaller: Current <= Result;
  125.      close_enough: Result.to_real - Current < one;
  126.       end;
  127.  
  128.    rounded: INTEGER is
  129.            -- Rounded integral value.
  130.       do
  131.      Result := to_double.rounded;
  132.       end;
  133.  
  134.    truncated_to_integer: INTEGER is
  135.            -- Integer part (same sign, largest absolute value
  136.             -- no greater than Current).
  137.       do
  138.      Result := to_double.truncated_to_integer;
  139.       ensure
  140.      Result.to_real <= Current
  141.       end;
  142.    
  143.    to_string: STRING is
  144.      -- Convert the REAL into a new allocated STRING. 
  145.      -- Note: see `append_in' to save memory.
  146.       do
  147.      Result := to_double.to_string;
  148.       end; 
  149.  
  150.    append_in(str: STRING) is
  151.      -- Append the equivalent of `to_string' at the end of 
  152.      -- `str'. Thus you can save memory because no other
  153.      -- STRING is allocate for the job.
  154.       require
  155.      str /= Void;
  156.       do
  157.      to_double.append_in(str);
  158.       end; 
  159.    
  160.    to_string_format(d: INTEGER): STRING is
  161.      -- Convert the REAL into a new allocated STRING including 
  162.      -- only `d' digits in fractionnal part. 
  163.      -- Note: see `append_in_format' to save memory.
  164.       do
  165.      Result := to_double.to_string_format(d);
  166.       end; 
  167.  
  168.    append_in_format(str: STRING; f: INTEGER) is
  169.      -- Same as `append_in' but produce only `f' digit of 
  170.      -- the fractionnal part.
  171.       require
  172.      str /= Void;
  173.      f >= 0
  174.       do
  175.      to_double.append_in_format(str,f);
  176.       end; 
  177.    
  178.    to_double: DOUBLE is
  179.       -- Note: C conversion from "float" to "double".
  180.       do
  181.      Result := Current;
  182.       end;
  183.    
  184. feature -- Maths functions :
  185.  
  186.    sqrt: DOUBLE is
  187.            -- Compute the square routine.
  188.       require
  189.      Current >= 0
  190.       do
  191.      Result := to_double.sqrt;
  192.       end;
  193.    
  194.    sin: DOUBLE is
  195.      -- Sinus (ANSI C sin).
  196.       do
  197.      Result := to_double.sin;
  198.       end;
  199.  
  200.    cos: DOUBLE is
  201.      -- Cosinus (ANSI C cos).
  202.       do
  203.      Result := to_double.cos;
  204.       end;
  205.  
  206.    tan: DOUBLE is
  207.      -- (ANSI C tan).
  208.       do
  209.      Result := to_double.tan;
  210.       end;
  211.  
  212.    asin: DOUBLE is
  213.      -- (ANSI C asin).
  214.       do
  215.      Result := to_double.asin;
  216.       end;
  217.  
  218.    acos: DOUBLE is
  219.      -- (ANSI C acos).
  220.       do
  221.      Result := to_double.acos;
  222.       end;
  223.  
  224.    atan: DOUBLE is
  225.      -- (ANSI C atan).
  226.       do
  227.      Result := to_double.atan;
  228.       end;
  229.  
  230.    sinh: DOUBLE is
  231.      -- (ANSI C sinh).
  232.       do
  233.      Result := to_double.sinh;
  234.       end;
  235.  
  236.    cosh: DOUBLE is
  237.      -- (ANSI C cosh).
  238.       do
  239.      Result := to_double.cosh;
  240.       end;
  241.  
  242.    tanh: DOUBLE is
  243.      -- (ANSI C tanh).
  244.       do
  245.      Result := to_double.tanh;
  246.       end;
  247.  
  248.    exp: DOUBLE is
  249.      -- (ANSI C exp).
  250.       do
  251.      Result := to_double.exp;
  252.       end;
  253.  
  254.    log: DOUBLE is
  255.      -- (ANSI C log).
  256.       do
  257.      Result := to_double.log;
  258.       end;
  259.  
  260.    log10: DOUBLE is
  261.      -- (ANSI C log10).
  262.       do
  263.      Result := to_double.log10;
  264.       end;
  265.  
  266. feature -- Object Printing :
  267.  
  268.    out_in_tagged_out_memory, fill_tagged_out_memory is
  269.       do
  270.      Current.append_in(tagged_out_memory);
  271.       end;
  272.  
  273. end -- REAL
  274.  
  275.